package main

import (
        "fmt"
	//"log"
	"net/http"
	"encoding/json"
	"io/ioutil"
	//"time"
)

type Atmosphere struct
{
	Taskid string `json:"taskid"`
	AtomsphereData
}

type AtomsphereData struct
{
	Position_num string `json:"positionnum"`
	Temperature float32 `json:"temperature"`
	Moisture float32 `json:"moisture"`
	Timestamp int64 `json:"timestamp"`
}

type RespMessage struct
{
	Taskid string
	Status string
}

func startHttpServer(w http.ResponseWriter, r *http.Request) {
	s, _ := ioutil.ReadAll(r.Body)
	
	var atmo_data Atmosphere 
	err := json.Unmarshal(s, &atmo_data)

	var resp_message RespMessage
	if err != nil {
		resp_message.Taskid = atmo_data.Taskid
		resp_message.Status = "data transfer failed."
	} else {
		resp_message.Taskid = atmo_data.Taskid
		resp_message.Status = "data transfer success."
	}
	b, err := json.Marshal(resp_message)

	fmt.Println(atmo_data.AtomsphereData.Timestamp)
	
	fmt.Fprintf(w, "%s", b)
}

func main() {
	http.HandleFunc("/", startHttpServer)
	http.ListenAndServe("127.0.0.1:8282", nil)
}
